#include "apue.h"
#include <pthread.h>

int main(void) {
    int err;
    struct timespec tout;
    struct tm *tmp;
    char buf[64];
    pthread_mutex_t lock= PTHREAD_MUTEX_INITIALIZER;
    int e;
    char * e_str;

    pthread_mutex_lock(&lock);
    printf("mutex is locked\n");
    clock_gettime(CLOCK_REALTIME, &tout);
    tmp= localtime(&tout.tv_sec);
    strftime(buf, sizeof(buf), "%r", tmp);
    printf("Now is %s\n", buf);

    tout.tv_sec += 10;
    if ((e= pthread_mutex_timedlock(&lock, &tout)) != 0) {
        e_str= strerror(e);
        printf("clock error: %s\n", e_str);
    }

    pthread_mutex_unlock(&lock);
    pthread_mutex_destroy(&lock);

    clock_gettime(CLOCK_REALTIME, &tout);
    tmp= localtime(&tout.tv_sec);
    strftime(buf, sizeof(buf), "%r", tmp);
    printf("Now is %s\n", buf);
    printf("end\n");
    exit(0);
}

